#include<iostream>
#include<vector>
#include<algorithm>
#include <string>
using namespace std;


struct ListNode {
    int val;
    struct ListNode* next;
    ListNode(int x) :
        val(x), next(NULL) {}
};

class Solution {
public:
    vector<int> printListFromTailToHead(ListNode* head) {
        vector<int> result;
        ListNode* t = head;
        while (t != NULL) {
            result.push_back(t->val);
            t = t->next;
        }
        reverse(result.begin(), result.end());
        return result;
    }
};

int main() {
    Solution s;
    ListNode  n1(1), n2(2), n3(3);
    n1.next = &n2, n2.next = &n3;
    vector<int> res = s.printListFromTailToHead(&n1);
    for (auto i : res)
        cout << i << " ";
}